//调整页面区域高度------------------------------------------
var newsheight=document.getElementsByClassName('newslistplace')[0].offsetTop;//新闻距离页面顶部高度
var windowheight=window.screen.availHeight;
document.getElementsByClassName('keywordlistplace')[0].style.height=windowheight-newsheight-46+"px";//网址 关键词公用高度
document.getElementsByClassName('urllistplace')[0].style.height=windowheight-newsheight-46+"px";//网址 关键词公用高度
document.getElementsByClassName('newslistplace')[0].style.height=windowheight-newsheight-15+"px";//新闻列表区高度

//nav按钮页面点击切换---------------------------------------
document.getElementById("gobackbtn").onclick=function(){
    history.go(-1);
};
$("#shieldnewsbtn").click(function(){
    $("#newsplace").css("display","block");
    $("#urlplace").css("display","none");
    $("#keywordplace").css("display","none");
    document.getElementById("shieldnewsbtn").className+=" active";//t.className = t.className.replace('Large', '');
    document.getElementById("shieldurlbtn").className="";
    document.getElementById("shieldkeywordbtn").className="";
});
$("#shieldurlbtn").click(function(){
    $("#newsplace").css("display","none");
    $("#urlplace").css("display","block");
    $("#keywordplace").css("display","none");
    document.getElementById("shieldnewsbtn").className="";
    document.getElementById("shieldurlbtn").className+=" active";
    document.getElementById("shieldkeywordbtn").className="";
});
$("#shieldkeywordbtn").click(function(){
    $("#newsplace").css("display","none");
    $("#urlplace").css("display","none");
    $("#keywordplace").css("display","block");
    document.getElementById("shieldnewsbtn").className="";
    document.getElementById("shieldurlbtn").className="";
    document.getElementById("shieldkeywordbtn").className+=" active";
});
//保存所有变量----------------------------------------------
var keywordcurrentIndex=1;
var newscurrentIndex=1;
var urlcurrentIndex=1;
var pageSize=30;
var newsover=0;//控制新闻滚动状态  0可以滚动加载
var urlsover=0;//控制网址滚动状态  0可以滚动加载
var keysover=0;//控制关键词滚动状态  0可以滚动加载
var newsnum=0;//已经屏蔽的新闻个数
var urlsnum=0;//已经屏蔽的网址个数
var keysnum=0;//已经屏蔽的关键词个数
//页面加载所有列表------------------------------------------
$().ready(function(){
    //获取屏蔽数量
    $.ajax({
        type:"get",
        datatype:"json",
        url:getshieldnumurl+id,
        success:function(d){
            console.log("获取屏蔽数量");
            console.log(d);
            newsnum= d.Data.newsCount;//已经屏蔽的新闻个数
            urlsnum= d.Data.siteCount;//已经屏蔽的网址个数
            //urlsnum= 100;//已经屏蔽的网址个数
            keysnum= d.Data.keyWordsCount;//已经屏蔽的关键词个数
            $("#keywordplace .tit span.num").html(keysnum);
            $("#urlplace .tit span.num").html(urlsnum);
            $("#newsplace .tit span.num").html(newsnum);
            maxnum();
        }
    });
    //获得列表信息
    getkeyword(keywordcurrentIndex);
    getnews(newscurrentIndex);
    geturl(urlcurrentIndex);
});
//判断屏蔽个数是否达到上限----------------------------------
function maxnum(){
    if(parseFloat(urlsnum)>=100){
        $("#addurlinput").attr({"disabled":"disabled"});
        $("#addurlinput").attr('placeholder','已达到屏蔽上限');
        $("#addurlbtn").css("display","none");
    }else{
        $("#addurlinput").removeAttr("disabled");
        $("#addurlinput").attr('placeholder','输入您想屏蔽的网站地址');
        $("#addurlbtn").css("display","block");
    }
    if(parseFloat(keysnum)>=100){
        $("#addkeywordinput").attr({"disabled":"disabled"});
        $("#addkeywordinput").attr('placeholder','已达到屏蔽上限');
        $("#addkeywordbtn").css("display","none");
    }else{
        $("#addkeywordinput").removeAttr("disabled");
        $("#addkeywordinput").attr('placeholder','输入您想屏蔽的关键词');
        $("#addkeywordbtn").css("display","block")
    }
}
//获取关键词------------------------------------------------
var getkeywordstatus="over";
function getkeyword(keywordcurrentIndex){
    getkeywordstatus="running";
    $.ajax({
        type:"get",
        datatype:"json",
        url:getshieldurl+"?userId="+id+"&shieldtype=1&currentIndex="+keywordcurrentIndex+"&pageSize="+pageSize,
        success:function(d){
            console.log("关键词");
            console.log(d);
            var tim=0;
            if(d.Data&& d.Data.length>0){
                $(".keywordlistplace div.noone").remove();
                $d=$(d.Data);
                $d.each(function(){
                    tim++;
                    var isid="key"+$(this)[0].id;
                    $(".keywordlistplace").append("<div class='item' id='"+isid+"'> <span>"+$(this)[0].content+"</span> <a href='javascript: ;' id='d"+$(this)[0].id+"' onclick='cancelshield(this)' data-id='"+isid+"'>解除 </a> </div>");
                    if(tim==d.Data.length){
                        getkeywordstatus="over";
                    }
                });
                keyover=0;
            }else{
                getkeywordstatus="over";
            }
        }
    });
}
//获取新闻--------------------------------------------------
var getnewsstatus="over";
function getnews(newscurrentIndex){
    getnewsstatus="running";
    $.ajax({
        type:"get",
        datatype:"json",
        url:getshieldurl+"?userId="+id+"&shieldtype=3&currentIndex="+newscurrentIndex+"&pageSize="+pageSize,
        success:function(d){
            console.log("新闻");
            console.log(d);
            if(d.Data&& d.Data.length>0){
                $(".newslistplace div.noone").remove();
                $d=$(d.Data);
                var tim=0;
                $d.each(function(){
                    tim++;
                    var isid="news"+$(this)[0].id;
                    $(".newslistplace").append("<div class='item' id='"+isid+"'> <div>"+$(this)[0].title+"</div><a href='javascript: ;' id='d"+$(this)[0].id+"' onclick='cancelshield(this)' data-id='"+isid+"'>解除 </a></div>");
                    if(tim===d.Data.length){
                        getnewsstatus="over";
                    }
                });
                newsover=0;
            }else{
                getnewsstatus="over";
            }
        }
    });
}
//获取网址--------------------------------------------------
var geturlstatus="over";
function geturl(urlcurrentIndex){
    geturlstatus="running";
    $.ajax({
        type:"get",
        datatype:"json",
        url:getshieldurl+"?userId="+id+"&shieldtype=2&currentIndex="+urlcurrentIndex+"&pageSize="+pageSize,
        success:function(d){
            console.log("网址");
            console.log(d);
            if(d.Data&& d.Data.length>0){
                $(".urllistplace div.noone").remove();
                $d=$(d.Data);
                var tim=0;
                $d.each(function(){
                    tim++;
                    var isid="url"+$(this)[0].id;
                    $(".urllistplace").append("<div class='item' id='"+isid+"'><span>"+$(this)[0].siteName+"</span><a href='javascript: ;' id='d"+$(this)[0].id+"' onclick='cancelshield(this)' data-id='"+isid+"'>解除 </a> </div>");
                    if(tim== d.Data.length){
                        geturlstatus="over";
                    }
                });
                urlover=0;
            }else{
                geturlstatus="over";
            }
        }
    });
}
//取消屏蔽--------------------------------------------------
function cancelshield(e){
    var shieldId=e.id.match(/(\d)+/)[0];
    var dele=e.dataset.id;
    console.log(shieldId);
    console.log(dele);
    console.log(getkeywordstatus,getnewsstatus,geturlstatus);
    if(getkeywordstatus==="over"&&getnewsstatus==="over"&&geturlstatus==="over"){
        $.ajax({
            type:"post",
            datatype:"json",
            url:cancelshieldurl,
            data:{"shieldId":shieldId},
            success:function(d){
                //新闻个数变化
                if(document.getElementById("newsplace").style.display==="block"){
                    if(getnewsstatus=="over"){
                        console.log(d);
                        $("#"+dele).remove();
                        var numnews=parseFloat($("#newsplace .tit .num").html());
                        --newsnum;
                        $("#newsplace .tit .num").html(newsnum);
                        if(newsnum==0){
                            $(".newslistplace div.noone").css("display","block");
                        }
                        //页面剩余个数加载更多
                        if($(".newslistplace .item")&&$(".newslistplace .item").length<7){
                            console.log($(".newslistplace .item").length);
                            newscurrentIndex++;
                            getnews(newscurrentIndex);
                            newsover=1
                        }
                    }
                }else  if(document.getElementById("urlplace").style.display==="block"){
                    if(geturlstatus=="over"){
                        console.log(d);
                        $("#"+dele).remove();
                        var numurl=parseFloat($("#urlplace .tit .num").html());
                        --urlsnum;
                        $("#urlplace .tit .num").html(urlsnum);
                        maxnum();
                        if(urlsnum==0){
                            $(".urllistplace div.noone").css("display","block");
                        }
                        //页面剩余个数加载更多
                        if($(".urllistplace .item")&&$(".urllistplace .item").length<7){
                            urlcurrentIndex++;
                            geturl(urlcurrentIndex);
                            urlover=1;

                        }
                    }
                }else if(document.getElementById("keywordplace").style.display==="block"){
                    if(getkeywordstatus=="over"){
                        console.log(d);
                        $("#"+dele).remove();
                        var numkey=parseFloat($("#keywordplace .tit .num").html());
                        --keysnum;
                        $("#keywordplace .tit .num").html(keysnum);
                        maxnum();
                        if(keysnum==0){
                            $(".keywordlistplace div.noone").css("display","block");
                        }
                        //页面剩余个数加载更多
                        if($(".keywordlistplace .item")&&$(".keywordlistplace .item").length<7){
                            keywordcurrentIndex++;
                            if(getkeywordstatus=="over"){
                                getkeyword(keywordcurrentIndex);
                                keysover=1;
                            }
                        }
                    }
                }
            }
        })
    }
}
//添加屏蔽关键词----------------------------------------------
$("#addkeywordbtn").click(function(){
    if($("#addkeywordinput").val()&&!$("#addkeywordinput").val().match(/\s/)&&$("#addkeywordinput").val().length>0){
        $.ajax({
            type:"post",
            datatype:"json",
            url:shieldurl,
            data:{"userId":id,"content":$("#addkeywordinput").val(),"shieldtype":1},
            success:function(da){
                console.log("屏蔽关键词");
                console.log(da);
                $("#addkeywordinput").val("");
                var isid="key"+da.Data.id;
                $(".keywordlistplace").prepend("<div class='item' id='"+isid+"'> <span>"+da.Data.content+"</span> <a href='javascript: ;' id='d"+da.Data.id+"' onclick='cancelshield(this)' data-id='"+isid+"'>解除 </a> </div>");
                ++keysnum;
                $("#keywordplace .tit .num").html(keysnum);
                maxnum();
                $(".keywordlistplace div.noone").css("display","none");
            }
        })
    }else{
        $("#keywordplace .label-info").html("格式不正确，请重新输入");
    }
});
$(function(){
    $("#addkeywordinput").bind('input propertychange', function() {
        $("#keywordplace .label-info").html("每个关键词长度不能超过10个汉字，不能包含空格");
    });
});
//添加屏蔽网址------------------------------------------------
$("#addurlbtn").click(function(){
    if($("#addurlinput").val()&&$("#addurlinput").val().match(/(http|ftp|https):\/\/([\w\-_]+|m)(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#],([^\s]))/)&&$("#addurlinput").val().length>0){
        console.log($("#addurlinput").val());
        //(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#],([\u4e00-\u9fa5]))?
        $.ajax({
            type:"post",
            datatype:"json",
            url:shieldurl,
            data:{"userId":id,"content":$("#addurlinput").val(),"shieldtype":2},
            success:function(da){
                console.log("屏蔽网址");
                console.log(da);
                $("#addurlinput").val("");
                var isid="url"+da.Data.id;
                var content=da.Data.content.match(/,([^\s]+)/).splice(",")[1];
                console.log(content);
                $(".urllistplace").prepend("<div class='item' id='"+isid+"'> <span>"+content+"</span> <a href='javascript: ;' id='d"+da.Data.id+"' onclick='cancelshield(this)' data-id='"+isid+"'>解除 </a> </div>");
                ++urlsnum;
                $("#urlplace .tit .num").html(urlsnum);
                maxnum();
            }
        })
    }else{
        $("#urlplace .label-info").html("格式不正确，请重新输入");
    }
});
$(function(){
    $("#addurlinput").bind('input propertychange', function() {
        $("#urlplace .label-info").html("网址与名称之间请用 , 分隔  例：http://www.xxx.com,某一网");
    });
});
//屏幕滚动----------------------------------------------------
//新闻页面滚动
$("#newsplace .newslistplace").scroll(function(){
    if(newsover===0){
        var lastheight=$("#newsplace .newslistplace .item:last-child").offset().top;
        if(lastheight<windowheight){
            newscurrentIndex++;
            getnews(newscurrentIndex);
            newsover=1;
        }
    }

});
//网址页面滚动
$("#urlplace .urllistplace").scroll(function(){
    if(urlover===0){
        var lastheight=$("#urlplace .urllistplace .item:last-child").offset().top;
        if(lastheight<windowheight){
            urlcurrentIndex++;
            geturl(urlcurrentIndex);
            urlover=1;
        }
    }
});
//关键词页面滚动
$("#keywordplace .keywordlistplace").scroll(function(){
    if(keyover===0){
        var lastheight=$("#keywordplace .keywordlistplace .item:last-child").offset().top;
        if(lastheight<windowheight){
            keywordcurrentIndex++;
            getkeyword(keywordcurrentIndex);
            keysover=1;
        }
    }
});